<?php
/**
 * 卓创网采集器
 */
namespace Facebook\WebDriver;

use Facebook\WebDriver\Chrome\ChromeOptions;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\Remote\WebDriverCapabilityType;

require_once('vendor/autoload.php');

$host = 'http://localhost:4444/wd/hub'; // this is the default
$capabilities = DesiredCapabilities::chrome();
//设置代理
$capabilities->setCapability(WebDriverCapabilityType::PROXY, [
    'proxyType' => 'manual',
    'httpProxy' => '127.0.0.1:2043',
    'sslProxy' => '127.0.0.1:2043',
]);
$driver = RemoteWebDriver::create($host, $capabilities, 5000);
$size = new WebDriverDimension(1280, 800);
$driver->manage()->window()->setSize($size);
//先登录
$driver->get('http://price.sci99.com/register.aspx');
//输入用户名和密码
$usernameInput = $driver->findElement(WebDriverBy::id('SciName'));
$passwordInput = $driver->findElement(WebDriverBy::id('SciPwd'));
$loginBtn = $driver->findElement(WebDriverBy::id('Btn_Login'));
$usernameInput->sendKeys('mondagroup');
$passwordInput->sendKeys('Monda123');
$loginBtn->click();

$driver->get("http://price.sci99.com/plastic.aspx?pagename=plastic");
// wait at most 10 seconds until at least one result is shown
try {
    $driver->wait(10)->until(
        WebDriverExpectedCondition::presenceOfElementLocated(
            WebDriverBy::className('gray-bottom-border')
        )
    );

    $links = $driver->findElements(WebDriverBy::cssSelector('a[target="priceList"]'));
    foreach($links as $value) {
        $driver->get($value->getAttribute('href'));

        $driver->wait(10)->until(
            WebDriverExpectedCondition::presenceOfElementLocated(
                WebDriverBy::className('pagination')
            )
        );
        //获取下一页的分页按钮
        while(($nextPageBtn = $driver->findElement(WebDriverBy::id('PagePaper_Next')))) {
            $nextPageBtn->click();
            sleep(1);
        }
    }


} catch (\Exception $e) {

}
// close the browser
//$driver->quit();
